/*
 * Copyright (c) 2007, Justin R. Akehurst
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this 
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, this 
 *    list of conditions and the following disclaimer in the documentation and/or other 
 *    materials provided with the distribution.
 *  * Neither the name of Poindextrose nor the names of its contributors may be 
 *    used to endorse or promote products derived from this software without specific 
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.apache.tools.ant.types.resources.comparators;

import org.apache.tools.ant.types.Resource;

public class IntegerPrefix extends ResourceComparator {
    public static final String ASCENDING = "ascending";
    public static final String DESCENDING = "descending";
    private static final String DEFAULT_DIRECTION = ASCENDING;
    private String direction = DEFAULT_DIRECTION;
    
    /* (non-Javadoc)
     * @see org.apache.tools.ant.types.resources.comparators.ResourceComparator#resourceCompare(org.apache.tools.ant.types.Resource, org.apache.tools.ant.types.Resource)
     */
    protected int resourceCompare(Resource foo, Resource bar) {       
        int result = getPrefix(foo).compareTo(getPrefix(bar));
        return (direction.equals(ASCENDING)) ? result : (result * -1);
    }
    
    /**
     * @param rsrc
     * @return
     */
    protected Integer getPrefix(Resource rsrc) {
        Integer prefix = prefixFromString(rsrc.getName());
        if (prefix == null) {
            prefix = new Integer(0);
        }
        return prefix;
    }
    
    /**
     * Set the sort direction for this IntegerPrefix ResourceComparator. If this
     * attribute is set to 'ascending', Resource content will be compared
     * in ascending integer order. If attribute is set to 'descending', Resource content
     * will be compared in descending integer order.
     * Default is <code>ascending</code>.
     * @param direction direction to compare and sort in integer order.
     */
    public void setDirection(String direction) {
        if (direction.equalsIgnoreCase(ASCENDING)) {
            this.direction = ASCENDING;
        } else if (direction.equalsIgnoreCase(DESCENDING)) {
            this.direction = DESCENDING;
        } else {
            this.direction = DEFAULT_DIRECTION;
        }
    }

    /**
     * Learn if this IntegerPrefix ResourceComparator is sorted ascending or descending.
     * @return String direction of sort.
     */
    public String getDirection() {
        return this.direction;
    }

    /**
     * @param str
     * @return
     */
    protected Integer prefixFromString(String str) {
        if (str == null) {
            return null;
        }
        String prefixStr = str.split("_", 2)[0];
        Integer prefix = null;
        try {
            prefix = new Integer(prefixStr);
        } catch (NumberFormatException e) {
            prefix = null;
        }
        return prefix;
    }
    
}
